﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web.UI.WebControls;

namespace QuanLyVanBanBUS
{
    public class NguoiDungBUS
    {
        public void themNguoiDung(NguoiDung ndDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            storeDB.NguoiDungs.InsertOnSubmit(ndDTO);

            storeDB.SubmitChanges();
        }

        public void suaNguoiDung(NguoiDung ndDTO, string maNguoiDung)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.NguoiDungs.Where(q => q.MaNguoiDung == maNguoiDung).First();
            query.TenNguoiDung = ndDTO.TenNguoiDung;
            query.MaLoaiNguoiDung = ndDTO.MaLoaiNguoiDung;
            query.MaPhong = ndDTO.MaPhong;
            query.MaChucVu = ndDTO.MaChucVu;
            query.MatKhau = ndDTO.MatKhau;
            query.DienThoaiDiDong = ndDTO.DienThoaiDiDong;
            query.DienThoaiNhaRieng = ndDTO.DienThoaiNhaRieng;
            query.Email = ndDTO.Email;
            query.BiXoa = ndDTO.BiXoa;

            storeDB.SubmitChanges();
        }

        public void xoaNguoiDung(GridView gridView)
        {
            //Lay MaLoaiNguoiDung duoc chon tu CheckBox
            string temp = string.Empty;

            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox _checkBox = (CheckBox)(gridView.Rows[i].Cells[0].FindControl("chkMaNguoiDung"));

                if (_checkBox.Checked == true)
                {
                    string maNguoiDungCanXoa = gridView.DataKeys[i].Value.ToString();

                    temp += maNguoiDungCanXoa + ",";
                }
            }

            //Kiem tra co check vao CheckBox khong?
            if (temp.Length - 1 > 0)
            {
                temp = temp.Substring(0, temp.Length - 1);
            }
            else
            {
                //Response.Write("<script>alert('Xin vui lòng chọn Sản phẩm cần xóa')</script>");

                return;
            }

            //Xoa
            QLVBDataContext storeDB = new QLVBDataContext();

            //Them cac MaLoaiNguoiDung duoc chon vao mang
            string[] m_temp = new string[] { temp };
            
            while (temp.Length > 0)
            {
                //Lay maTemp de lam dieu kien so sanh. Lay tu sau lay toi cua Mang
                string maTemp = temp.Substring(temp.LastIndexOf(",") + 1, temp.Length - (temp.LastIndexOf(",") + 1));

                var query = storeDB.NguoiDungs.Where(q => q.MaNguoiDung == maTemp).First();

                //Khong xoa khoi CSDL, khong cho hien thi bang cach: set BiXoa=1 
                query.BiXoa = 1;

                storeDB.SubmitChanges();

                //Kiem tra dieu kien co con trong vong lap While khong?
                if ((temp.Length - (maTemp.Length + 1)) > 0)
                {
                    temp = temp.Substring(0, temp.Length - (maTemp.Length + 1));
                }
                else
                    break;
            }

            gridView.DataSource = layDanhSachNguoiDung();
            gridView.DataBind();
        }

        public IList<NguoiDung> layDanhSachNguoiDung()
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var lstNguoiDung = storeDB.NguoiDungs.Where(l => l.BiXoa == 0).ToList();

            return lstNguoiDung;
        }

        public IList<NguoiDung> layDanhSachNguoiDungTheoMaPhong(string maPhong)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var lstNguoiDung = storeDB.NguoiDungs.Where(l => l.MaPhong == maPhong && l.BiXoa == 0).ToList();

            return lstNguoiDung;
        }
      
        public Boolean kiemtraTonTaiMaNguoiDung(string maNguoiDung)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.NguoiDungs.Where(q => q.MaNguoiDung == maNguoiDung).ToList();

            if (query.Count > 0)
            {
                return true;
            }

            return false;
        }

        public Boolean kiemtraTonTaiTenNguoiDung(string tenNguoiDung)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.NguoiDungs.Where(q => q.TenNguoiDung == tenNguoiDung).ToList();

            if (query.Count > 0)
            {
                return true;
            }

            return false;
        }

        public Boolean kiemtraTonTaiEmail(string email)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.NguoiDungs.Where(q => q.Email == email).ToList();

            if (query.Count > 0)
            {
                return true;
            }

            return false;
        }

        public string layMaNguoiDung(string maNguoiDung)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var _maNguoiDung = storeDB.NguoiDungs.Where(m => m.MaNguoiDung == maNguoiDung).Select(m => m.MaNguoiDung).First();

            return _maNguoiDung;
        }

        public string layTenNguoiDung(string maNguoiDung)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var tenNguoiDung = storeDB.NguoiDungs.Where(m => m.MaNguoiDung == maNguoiDung).Select(m => m.TenNguoiDung).First();

            return tenNguoiDung;
        }

        public string layMatKhau(string maNguoiDung)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var matKhau = storeDB.NguoiDungs.Where(m => m.MaNguoiDung == maNguoiDung).Select(m => m.MatKhau).First();

            return matKhau;
        }

        public string layDienThoaiDiDong(string maNguoiDung)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var dtdd = storeDB.NguoiDungs.Where(m => m.MaNguoiDung == maNguoiDung).Select(m => m.DienThoaiDiDong).First();

            return dtdd;
        }

        public string layDienThoaiNhaRieng(string maNguoiDung)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var dtnr = storeDB.NguoiDungs.Where(m => m.MaNguoiDung == maNguoiDung).Select(m => m.DienThoaiNhaRieng).First();

            return dtnr;
        }

        public string layEmail(string maNguoiDung)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var email = storeDB.NguoiDungs.Where(m => m.MaNguoiDung == maNguoiDung).Select(m => m.Email).First();

            return email;
        }

        //public void themItemVaoDropDownList(DropDownList dropDownList)
        //{   
        //    QLVBDataContext storeDB = new QLVBDataContext();

        //    var query = storeDB.NguoiDungs.ToList();
        //    foreach (var q in query)
        //    {
        //        var biXoa = q.BiXoa;

        //        ListItem item = new ListItem();
        //        if (biXoa == 0)
        //        {
        //            item.Text = "Chưa xóa";
        //        }
        //        else if (biXoa == 1)
        //        {
        //            item.Text = "Đã xóa";
        //        }
        //        item.Value = biXoa.ToString();

        //        dropDownList.Items.Add(item);
        //    }
        //}
        
        public void sapxepBiXoa(DropDownList dropDownList, string maNguoiDung)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var biXoa = storeDB.NguoiDungs.Where(m => m.MaNguoiDung == maNguoiDung).Select(m => m.BiXoa).First();

            dropDownList.SelectedValue = biXoa.ToString();
        }

        public IList<NguoiDung> timkiemMaNguoiDung(string maNguoiDung)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.NguoiDungs.Where(q => q.MaNguoiDung == maNguoiDung).ToList();

            return query;
        }

        public IList<NguoiDung> timkiemTenNguoiDung(string tenNguoiDung)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.NguoiDungs.Where(q => q.TenNguoiDung == tenNguoiDung).ToList();

            return query;
        }

        public void thaydoiMatKhau(string maNguoiDung, string matKhauMoi)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.NguoiDungs.Where(q => q.MaNguoiDung == maNguoiDung).First();

            query.MatKhau = matKhauMoi;

            storeDB.SubmitChanges();
        }

        //public string DangNhap(string maNguoiDung, string matKhau)
        //{
        //    QLVBDataContext storeDB = new QLVBDataContext();

        //    var query = storeDB.NguoiDungs.Where(q => q.MaNguoiDung == maNguoiDung && q.MatKhau == matKhau).ToList();

        //    if (query.Count > 0)
        //    {
        //        LoaiNguoiDungBUS lndBUS = new LoaiNguoiDungBUS();

        //        string maLoaiNguoiDung = lndBUS.layMaLoaiNguoiDungTheoMaNguoiDung(maNguoiDung);

        //        return maLoaiNguoiDung;
        //    }

        //    return null;
        //}

        public Boolean kiemtraDangNhap(string maNguoiDung, string matKhau)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.NguoiDungs.Where(q => q.MaNguoiDung == maNguoiDung && q.MatKhau == matKhau).ToList();

            if (query.Count > 0)
            {
                return true;
            }

            return false;
        }

        public string layMaLoaiNguoiDungTheoMaNguoiDung(string maNguoiDung)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = (from nd in storeDB.NguoiDungs
                         join lnd in storeDB.LoaiNguoiDungs on nd.MaLoaiNguoiDung equals lnd.MaLoaiNguoiDung
                         where nd.MaNguoiDung == maNguoiDung
                         select nd.MaLoaiNguoiDung).First();

            return query;
        }

        public IList<NguoiDung> layMaNguoiDungTheoSoVanBan(string soVanBan)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            VanBanBUS vbBUS = new VanBanBUS();
            string maVanBan = vbBUS.layMaVanBanTheoSoVanBan(soVanBan);

            var query = (from nn in storeDB.NoiNhans
                         join nd in storeDB.NguoiDungs on nn.MaNguoiNhan equals nd.MaNguoiDung
                         where nn.MaVanBan == maVanBan && nn.BiXoa == 0
                         select nd).ToList();

            return query;
        }

        public IList<NguoiDung> layTenNguoiDungTheoMaNguoiDung(string maNguoiDung)
        {
            QLVBDataContext storDB = new QLVBDataContext();

            var query = storDB.NguoiDungs.Where(q => q.MaNguoiDung == maNguoiDung).ToList();

            return query;
        }

        public IList<NguoiDung> layMaChucVuTheoMaPhong(string maPhong)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = (from nd in storeDB.NguoiDungs
                        join p in storeDB.Phongs on nd.MaPhong equals p.MaPhong
                        join cv in storeDB.ChucVus on nd.MaChucVu equals cv.MaChucVu
                        where p.MaPhong == maPhong
                        select nd).ToList();

            return query;
        }

        public String layMaNguoiDungTheoMaPhong(string maPhong)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = (from nd in storeDB.NguoiDungs
                         join p in storeDB.Phongs on nd.MaPhong equals p.MaPhong
                         where p.MaPhong == maPhong
                         select nd.MaNguoiDung).First();

            return query;
        }

        public String layMaNguoiDungTheoMaChucVu(string maChucVu)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.NguoiDungs.Where(q => q.MaChucVu == maChucVu).Select(q => q.MaNguoiDung).First();

            return query;
        }

        public void themMaNguoiNhanVanBan(GridView gridView, TextBox txtSoVanBan)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            NoiNhanBUS nnBUS = new NoiNhanBUS();

            VanBanBUS vbBUS = new VanBanBUS();
            
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox _checkBox = (CheckBox)(gridView.Rows[i].Cells[0].FindControl("chkMaPhong"));
                if (_checkBox.Checked == true)
                {
                    string maPhong = gridView.DataKeys[i].Value.ToString();

                    var query = storeDB.NguoiDungs.Where(q => (q.MaPhong == maPhong) && (q.MaChucVu == "GD" || q.MaChucVu == "PGD" || q.MaChucVu == "TP" || q.MaChucVu == "PTP" || q.MaChucVu == "TT" || q.MaChucVu == "DH")).ToList();                    
                    foreach (NguoiDung nd in query)
                    {
                        NoiNhan nnDTO = new NoiNhan();

                        string maNoiNhanMax;
                        if (nnBUS.layMaNoiNhanMax() != null || nnBUS.layMaNoiNhanMax() != string.Empty)
                        {
                            maNoiNhanMax = Convert.ToString(Convert.ToInt32(nnBUS.layMaNoiNhanMax()) + 1);
                            nnDTO.MaNoiNhan = DateTime.Now.Date.Year.ToString() + maNoiNhanMax.ToString();
                            if (nnDTO.MaNoiNhan.Length < 10)
                            {
                                //Viet ham them so luong "so 0" con thieu vao maNoiNhanMax
                                int soLuong = 10 - nnDTO.MaNoiNhan.Length;
                                maNoiNhanMax = themSo0(soLuong) + maNoiNhanMax;
                                nnDTO.MaNoiNhan = DateTime.Now.Date.Year.ToString() + maNoiNhanMax;
                            }
                        }
                        else
                        {
                            maNoiNhanMax = "000000";
                            nnDTO.MaNoiNhan = DateTime.Now.Date.Year.ToString() + maNoiNhanMax;
                        }

                        //Them
                        nnDTO.MaNguoiNhan = nd.MaNguoiDung;
                        nnDTO.MaVanBan = vbBUS.layMaVanBanMaxVuaThemVao(txtSoVanBan.Text);
                        nnDTO.NgayNhan = DateTime.Now;
                        nnDTO.KiemSoat = 0;
                        nnDTO.BiXoa = 0;
                        nnBUS.themNoiNhan(nnDTO);
                    }
                }
            }
        }

        protected string themSo0(int soLuong)
        {
            string temp = string.Empty;
            for (int i = 0; i < soLuong; i++)
            {
                temp += "0";
            }

            return temp;
        }

        public IList<NguoiDung> layMaNguoiDungTheoMaChucVuVaMaPhong(string maChucVu, string maPhong)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.NguoiDungs.Where(q => q.MaChucVu == maChucVu && q.MaPhong == maPhong).ToList();

            return query;
        }

        public void themMaVanBanTheoMaNguoiDung(GridView gridViewMaVanBan, GridView gridViewMaNguoiDung)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            NoiNhanBUS nnBUS = new NoiNhanBUS();

            VanBanBUS vbBUS = new VanBanBUS();

            for (int i = 0; i < gridViewMaVanBan.Rows.Count; i++)
            {
                CheckBox chkMaVanBan = (CheckBox)(gridViewMaVanBan.Rows[i].Cells[0].FindControl("chkMaVanBan"));
                if (chkMaVanBan.Checked == true)
                {
                    string maVanBan = gridViewMaVanBan.DataKeys[i].Value.ToString();

                    for (int j = 0; j < gridViewMaNguoiDung.Rows.Count; j++)
                    {
                        CheckBox chkMaNguoiDung = (CheckBox)(gridViewMaNguoiDung.Rows[j].Cells[0].FindControl("chkMaNguoiDung"));
                        if (chkMaNguoiDung.Checked == true)
                        {
                            string maNguoiDung = gridViewMaNguoiDung.DataKeys[j].Value.ToString();

                            bool kiemtra = nnBUS.kiemtraTonTaiMaVanBanTheoMaNguoiNhan(maVanBan, maNguoiDung);
                            if (kiemtra == false)
                            {
                                //Them vao bang NoiNhan
                                NoiNhan nnDTO = new NoiNhan();

                                string maNoiNhanMax;
                                if (nnBUS.layMaNoiNhanMax() != null || nnBUS.layMaNoiNhanMax() != string.Empty)
                                {
                                    maNoiNhanMax = Convert.ToString(Convert.ToInt32(nnBUS.layMaNoiNhanMax()) + 1);
                                    nnDTO.MaNoiNhan = DateTime.Now.Date.Year.ToString() + maNoiNhanMax.ToString();
                                    if (nnDTO.MaNoiNhan.Length < 10)
                                    {
                                        //Viet ham them so luong "so 0" con thieu vao maNoiNhanMax
                                        int soLuong = 10 - nnDTO.MaNoiNhan.Length;
                                        maNoiNhanMax = themSo0(soLuong) + maNoiNhanMax;
                                        nnDTO.MaNoiNhan = DateTime.Now.Date.Year.ToString() + maNoiNhanMax;
                                    }
                                }
                                else
                                {
                                    maNoiNhanMax = "000000";
                                    nnDTO.MaNoiNhan = DateTime.Now.Date.Year.ToString() + maNoiNhanMax;
                                }

                                string maNguoiNhan = maNguoiDung;
                                nnDTO.MaNguoiNhan = maNguoiNhan;
                                nnDTO.MaVanBan = maVanBan;
                                nnDTO.NgayNhan = DateTime.Now;
                                nnDTO.KiemSoat = 1;                 //Bat flag nay len 1 de cho nguoi nhan thay duoc van ban
                                nnDTO.BiXoa = 0;
                                nnBUS.themNoiNhan(nnDTO);
                            }
                        }
                    }
                }
            } 
        }

        public IList<NoiNhan> layDanhSachNguoiDungTheoMaVanBan(string soVanBan)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var maVanBan = storeDB.VanBans.Where(q => q.SoVanBan == soVanBan).Select(q => q.MaVanBan).First();

            var query = storeDB.NoiNhans.Where(q => q.MaVanBan == maVanBan && q.KiemSoat == 0 && q.BiXoa == 0).ToList();

            return query;
        }
    }
}
